package com.iteaj.network.test;

import com.iteaj.iot.client.DefaultClientComponent;
import com.iteaj.iot.client.TcpClientBuilder;
import com.iteaj.iot.client.TcpClientComponent;
import com.iteaj.network.AbstractProtocol;
import com.iteaj.network.ProtocolFactory;
import com.iteaj.network.codec.adapter.LineBasedFrameMessageDecoderAdapter;
import com.iteaj.network.config.ConnectProperties;
import com.iteaj.network.test.client.line.LineClientComponent;
import com.iteaj.network.test.client.line.LineClientMessage;
import com.iteaj.network.test.server.line.TestLineBasedFrameDecoderComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

//@ConditionalOnExpression("${iot.test.client}")
@EnableConfigurationProperties(IotTestProperties.class)
public class IotTestAutoConfiguration {

    @Autowired
    private IotTestProperties properties;

    @Bean
    @ConditionalOnExpression("${iot.test.server}")
    public TestLineBasedFrameDecoderComponent lineBasedFrameDecoderComponentTest() {
        return new TestLineBasedFrameDecoderComponent(properties.getLine());
    }

    @Bean
    public LineClientComponent lineClientComponent() {
        return new LineClientComponent(new LineBasedFrameMessageDecoderAdapter(1024)
                , new ConnectProperties("127.0.0.1", 15800));

//        return TcpClientBuilder.useLineBasedFrameDecoder().maxLength(1024).then()
//                .useFactory(new ProtocolFactory<LineClientMessage>() {
//                    @Override
//                    public AbstractProtocol getProtocol(LineClientMessage message) {
//                        return null;
//                    }
//                }).connect("127.0.0.1", 15800).profile("换行符解码测试").build();
    }
}
